package classes.pack;

import java.util.Vector;

import android.util.Log;

import com.google.android.maps.GeoPoint;

public class Logging 
{
	//AppStatus testStatus = AppStatus.NOT_PARKING;
	
	static Vector <LogRecord> localLogging ;
	
	public Logging()
	{
		//load elements from xml
		localLogging = new Vector<LogRecord>();
		//test 
		//load test elements to memory
		
		
		String tempStatus = Gui.getProfiling().loadPreferencesString("status");
		
		if(tempStatus == null || tempStatus == "")
		{//no data available
			updateStatus(AppStatus.NOT_PARKING, new GeoPoint(100, 100));
		}
		else
		{
			AppStatus tStatus = AppStatus.valueOf(tempStatus);
			int tLat = Gui.getProfiling().loadPreferencesInt("lat", 0);
			int tLon = Gui.getProfiling().loadPreferencesInt("lon", 0);
			Log.i("testProfiling", tempStatus);
			Log.i("testProfiling", "lat:" + Integer.toString(tLat));
			Log.i("testProfiling", "lon:" + Integer.toString(tLon));
			
			
			updateStatus(tStatus, new GeoPoint(tLat, tLon));
		}
		Log.i("testProfiling", tempStatus);
		//end test
	}
	
	public static void Initialize()
	{
		//load elements from xml
		localLogging = new Vector<LogRecord>();
		//test 
		//load test elements to memory
				
		String tempStatus = Gui.getProfiling().loadPreferencesString("status");
		
		if(tempStatus == null || tempStatus == "")
		{//no data available
			updateStatus(AppStatus.NOT_PARKING, new GeoPoint(100, 100));
		}
		else
		{
			AppStatus tStatus;
			
			if(tempStatus == "PARKING")
			{
				tStatus = AppStatus.PARKING;
			}
			else if(tempStatus == "FREE_PARKING")
			{
				tStatus = AppStatus.FREE_PARKING;
			}
			else
			{
				tStatus = AppStatus.NOT_PARKING;
			}
			
			int tLat = Gui.getProfiling().loadPreferencesInt("lat", 0);
			int tLon = Gui.getProfiling().loadPreferencesInt("lon", 0);
			Log.i("testProfiling", tempStatus);
			Log.i("testProfiling", "lat:" + Integer.toString(tLat));
			Log.i("testProfiling", "lon:" + Integer.toString(tLon));
			
			
			updateStatus(tStatus, new GeoPoint(tLat, tLon));
		}
		Log.i("testProfiling", tempStatus);
		//updateStatus(AppStatus.NOT_PARKING, new GeoPoint(100, 100));
	}
	
	/////////////////////
	////// GETTERS //////

	public static AppStatus getLatestStatus()
	{
		//loop through Log to find the latest status
		for(int i = localLogging.size() - 1; i>=0; i--)
		{
			if(localLogging.get(i).getLoggingType() == LoggingType.STATUS && localLogging.get(i).getStatus() != null)
			{	//when the latest status is found, return it 
				return localLogging.get(i).getStatus();
			}
			
		}
		//generate error to log
		
		return null;
		
		//return testStatus;	//Retrieve status from log	
	}
	
	public static LogRecord getLatestPosition()
	{
		//returns latest LogRecord with a position
		//loop through Log to find the latest position
		for(int i = localLogging.size() - 1; i>=0; i--)
		{
			if(localLogging.get(i).getLocation() != null)
			{	//when the latest status is found, return it 
				return localLogging.get(i);
			}
			
		}
		//generate error to log
		
		return null;
		
		//return testStatus;	//Retrieve status from log	
	}
	
	
	public static Vector<LogRecord> getLoggedPositions(int nr)
	{
		//Note! instead of only the position, the entire LogRecod object is retrned because of the timestamp involved
		//get the last nr of positions (if available)
		int cntr = 0;
		Vector<LogRecord> latestPositions = new Vector<LogRecord>();
		//loop through Log to find the latest status
		for(int i = localLogging.size() - 1; i>=0; i--)
		{
			if(cntr >= nr)
			{	
				return latestPositions;
			}
			
			if(localLogging.get(i).getLocation() != null)
			{	//when the latest status is found, return it 
				
				latestPositions.add(localLogging.get(i));
				cntr++;				
			}
			
		}
		
		
		return latestPositions; 
		
			
	}

	
	
	
	public static LogRecord getParkingSpot()
	{
		// search the log for the current parking position
		// the Method will search for the last status update that is PARKING, FREE_PARKING or NOT_PARKING 
		// NOTE: if the last status update was NOT_PARKING the method will assume you are not_parking and will return null 
		//		 if there is no record PARKING or FREE_PARKING the method will return null
		//
		
		for(int i = localLogging.size() - 1; i>=0; i--)
		{
			LogRecord tempLogRecord = localLogging.get(i);
			
			if(tempLogRecord.getLocation() != null)
			{
				if(tempLogRecord.getStatus() == AppStatus.NOT_PARKING)
				{
					return null;
				}
				else if(tempLogRecord.getStatus() == AppStatus.PARKING)
				{
					return tempLogRecord;
				}
				else if(tempLogRecord.getStatus() == AppStatus.FREE_PARKING)
				{
					return tempLogRecord;
				}				
				//when the latest status is found, return it 
				
			}
			
		}
		
		return null;
		
	}
	
	
	////// GETTERS //////
	/////////////////////
	
	/////////////////////
	////// SETTERS //////
	
	
	////// SETTERS //////
	/////////////////////
	
	public static void loadLogs()
	{
		//load logs from xml
	}

	public static void updateStatus(AppStatus status, GeoPoint g)
	{
		
		localLogging.add(new LogRecord(status, g));
		
		//save status to machine
		Gui.getProfiling().savePreferencesString("status", status.toString());
		Gui.getProfiling().savePreferencesInt("lat", g.getLatitudeE6());
		Gui.getProfiling().savePreferencesInt("lon", g.getLongitudeE6());
		//add status to log
	}
	
	public static void updatePosition(GeoPoint g)
	{
		localLogging.add(new LogRecord(g));
	}
	///////////////////////////
	////// ERROR LOGGING //////
	
	public static void logError(GeoPoint g, ErrorType errorType, String message)
	{
		String eMessage = errorType.toString() + " :" + message;
		localLogging.add(new LogRecord(g, eMessage));
	}
	
	////// ERROR LOGGING //////
	///////////////////////////

	
	
	//////////////////
	////// TEST //////
	
	public static void testShowLogs()
	{
		Log.i("LOG", "Showing all logs");
		
		for(int i = 0; i < localLogging.size(); i++)
		{
			LogRecord tempRecord = localLogging.get(i);
			
			
			
			String tempStatus = "NULL";
			if(tempRecord.getStatus() != null)
			{
				tempStatus = tempRecord.getStatus().toString();
			}
			
			String tempLoc = "NULL";
			if(tempRecord.getLocation() != null)
			{
				tempLoc = tempRecord.getLocation().toString();
			}
			
			
			String tempDateTime = "NULL";
			if(tempRecord.getDateTime() != null)
			{
				tempDateTime = tempRecord.getDateTime().toString();
			}
			
			
			Log.i("LOG "+i, " status: "+ tempStatus + " loc: " + tempLoc + "date/time: " + tempDateTime);
		}
		
		Log.i("LOG", "End Showing all logs");
	}
	
	public static void testShowStatusLogs()
	{
		Log.i("LOG", "Showing all status logs");
		
		for(int i = 0; i < localLogging.size(); i++)
		{
			if(localLogging.get(i).getLoggingType() == LoggingType.STATUS)
			{
				LogRecord tempRecord = localLogging.get(i);
			
				String tempStatus = "NULL";
				if(tempRecord.getStatus() != null)
				{
					tempStatus = tempRecord.getStatus().toString();
				}
				
				String tempLoc = "NULL";
				if(tempRecord.getLocation() != null)
				{
					tempLoc = tempRecord.getLocation().toString();
				}
				
				
				String tempDateTime = "NULL";
				if(tempRecord.getDateTime() != null)
				{
					tempDateTime = tempRecord.getDateTime().toString();
				}
								
				Log.i("LOG "+i, " status: "+tempStatus + " loc: " + tempLoc + " date/time: " + tempDateTime);

			}
		}
		
		Log.i("LOG", "End Showing all status logs");
	}
	
	public static void testShowPositionLogs()
	{
		Log.i("LOG", "Showing all position logs");
		
		for(int i = 0; i < localLogging.size(); i++)
		{
			if((localLogging.get(i).getLoggingType() == LoggingType.STATUS || localLogging.get(i).getLoggingType() == LoggingType.POSITION_UPDATE) && localLogging.get(i).getLocation() != null)
			{
				LogRecord tempRecord = localLogging.get(i);
				Log.i("LOG "+i, "status: "+tempRecord.getStatus().toString() + " loc: " + tempRecord.getLocation().toString() + "date/time: " + tempRecord.getDateTime());
			}
		}
		
		Log.i("LOG", "End Showing all position logs");
	}
	
	
	public static void testShowErrorLogs()
	{
		Log.i("LOG", "Showing all error logs");
		
		for(int i = 0; i < localLogging.size(); i++)
		{
			if(localLogging.get(i).getLoggingType() == LoggingType.ERROR)
			{
				LogRecord tempRecord = localLogging.get(i);
		
				
				String tempStatus = "NULL";
				if(tempRecord.getStatus() != null)
				{
					tempStatus = tempRecord.getStatus().toString();
				}
				
				String tempLoc = "NULL";
				if(tempRecord.getLocation() != null)
				{
					tempLoc = tempRecord.getLocation().toString();
				}
				
				
				String tempDateTime = "NULL";
				if(tempRecord.getDateTime() != null)
				{
					tempDateTime = tempRecord.getDateTime().toString();
				}
				
				String tempMessage = "NULL";
				if(tempRecord.getMessage() != null)
				{
					tempDateTime = tempRecord.getMessage();
				}
				
				
				Log.i("LOG "+i, " status: "+tempStatus + " loc: " + tempLoc + " date/time: " + tempDateTime + " message: " + tempMessage);
				
			}
		}
		
		Log.i("LOG", "End Showing all error logs");
	}
	////// TEST //////
	//////////////////
	
}
